package br.com.arosa.dao;

import java.util.*;
import java.sql.*;
import br.com.easynet.database.DataSet;
import br.com.jdragon.dao.*;
import br.com.arosa.transfer.*;

public class Tb_vendaDAO extends ObjectDAO { 
	 public Tb_vendaDAO(DAOFactory dao) throws Exception {
		 setDAOFactory(dao);
 		 con = dao.create();
 	 }

	 public void insert(Tb_vendaT tb_vendaT ) throws Exception { 
		 PreparedStatement pStmt=null;
		 try {
			 String sql = "insert into tb_venda  ( id_venda, id_cliente, id_forma_pagamento, data_venda, tipo_desconto, desconto) values ( ? , ? , ? , ? , ? , ? )";
			 pStmt = con.prepareStatement(sql);
			 pStmt.setObject(1, tb_vendaT.getId_venda());
			 pStmt.setObject(2, tb_vendaT.getId_cliente());
			 pStmt.setObject(3, tb_vendaT.getId_forma_pagamento());
		 java.sql.Date dt4= new java.sql.Date(tb_vendaT.getData_venda().getTime());
			 pStmt.setObject(4, dt4);
			 pStmt.setObject(5, tb_vendaT.getTipo_desconto());
			 pStmt.setObject(6, tb_vendaT.getDesconto());
			 pStmt.execute();
 		 } catch (Exception e) {
 			  throw e;
 		} finally {
 			 try { 
 			 pStmt.close();} catch (Exception e) {}
 			  
 		} 
 	} 

 	 public void update(Tb_vendaT tb_vendaT ) throws Exception { 
		 PreparedStatement pStmt=null;
		 try {
			 String sql = "update tb_venda set  id_cliente=?, id_forma_pagamento=?, data_venda=?, tipo_desconto=?, desconto=?  where  id_venda=?";
 			 pStmt = con.prepareStatement(sql);
			 pStmt.setObject(1, tb_vendaT.getId_cliente());
			 pStmt.setObject(2, tb_vendaT.getId_forma_pagamento());
		 java.sql.Date dt3= new java.sql.Date(tb_vendaT.getData_venda().getTime());
			 pStmt.setObject(3, dt3);
			 pStmt.setObject(4, tb_vendaT.getTipo_desconto());
			 pStmt.setObject(5, tb_vendaT.getDesconto());
			 pStmt.setObject(6, tb_vendaT.getId_venda());
			 pStmt.execute();
 		 } catch (Exception e) {
 			  throw e;
 		} finally {
 			 try { 
 			 pStmt.close();} catch (Exception e) {}
 			  
 		} 
 	} 

 	 public void delete(Tb_vendaT tb_vendaT ) throws Exception { 
		 PreparedStatement pStmt=null;
		 try {
			 String sql = "delete from tb_venda where  id_venda=?";
			 pStmt = con.prepareStatement(sql);
			 pStmt.setObject(1, tb_vendaT.getId_venda());
			 pStmt.execute();
		 } catch (Exception e)  {
 			  throw e;
 		} finally {
 			 try { 
 			 pStmt.close();} catch (Exception e) {}
 			  
 		} 
 	} 

 	 private List<Tb_vendaT> resultSetToObjectTransfer(ResultSet rs) throws Exception { 
		 List<Tb_vendaT> objs = new Vector();
		 while (rs.next()) { 
 			 Tb_vendaT tb_vendaT = new Tb_vendaT();
			 tb_vendaT.setId_venda(rs.getInt("id_venda"));
			 tb_vendaT.setId_cliente(rs.getInt("id_cliente"));
			 tb_vendaT.setId_forma_pagamento(rs.getInt("id_forma_pagamento"));
			 tb_vendaT.setData_venda(rs.getDate("data_venda"));
			 tb_vendaT.setTipo_desconto(rs.getInt("tipo_desconto"));
			 tb_vendaT.setDesconto(rs.getInt("desconto"));
			 objs.add(tb_vendaT);
 		 }
 		 return objs; 
 	 }
	 public List<Tb_vendaT> getAll() throws Exception { 
		 PreparedStatement pStmt=null;
		 ResultSet rs=null;
		 try {
			 String sql = "select * from tb_venda"; 
			 pStmt = con.prepareStatement(sql);
			 rs = pStmt.executeQuery();
 			 List<Tb_vendaT> list = resultSetToObjectTransfer(rs);
 			 return list; 
 		 } catch (Exception e) {
 			  throw e;
 		} finally {
 			 try { rs.close();
 			 pStmt.close();} catch (Exception e) {}
 			  
 		} 
 	} 

 	 public List<Tb_vendaT> getById(Tb_vendaT tb_vendaT) throws Exception { 
		 PreparedStatement pStmt=null;
		 ResultSet rs=null;
		 try {
			 String sql = "select * from tb_venda where  id_venda=?"; 
			 pStmt = con.prepareStatement(sql);
			 pStmt.setObject(1, tb_vendaT.getId_venda());
			 rs = pStmt.executeQuery();
 			 return resultSetToObjectTransfer(rs); 
 		 } catch (Exception e) {
 			  throw e;
 		} finally {
 			 try { rs.close();
 			 pStmt.close();} catch (Exception e) {}
 			  
 		} 
 	} 

 	 public List<Tb_vendaT> getById_venda(Tb_vendaT tb_vendaT) throws Exception { 
		 PreparedStatement pStmt=null;
		 ResultSet rs=null;
		 try {
			 String sql = "select * from tb_venda where  id_venda = ? "; 
			 pStmt = con.prepareStatement(sql);
			 pStmt.setObject(1, tb_vendaT.getId_venda());
			 rs = pStmt.executeQuery();
 			 return resultSetToObjectTransfer(rs); 
 		 } catch (Exception e) {
 			  throw e;
 		} finally {
 			 try { rs.close();
 			 pStmt.close();} catch (Exception e) {}
 			  
 		} 
 	} 

 	 public List<Tb_vendaT> getById_cliente(Tb_vendaT tb_vendaT) throws Exception { 
		 PreparedStatement pStmt=null;
		 ResultSet rs=null;
		 try {
			 String sql = "select * from tb_venda where  id_cliente = ? "; 
			 pStmt = con.prepareStatement(sql);
			 pStmt.setObject(1, tb_vendaT.getId_cliente());
			 rs = pStmt.executeQuery();
 			 return resultSetToObjectTransfer(rs); 
 		 } catch (Exception e) {
 			  throw e;
 		} finally {
 			 try { rs.close();
 			 pStmt.close();} catch (Exception e) {}
 			  
 		} 
 	} 

 	 public List<Tb_vendaT> getById_forma_pagamento(Tb_vendaT tb_vendaT) throws Exception { 
		 PreparedStatement pStmt=null;
		 ResultSet rs=null;
		 try {
			 String sql = "select * from tb_venda where  id_forma_pagamento = ? "; 
			 pStmt = con.prepareStatement(sql);
			 pStmt.setObject(1, tb_vendaT.getId_forma_pagamento());
			 rs = pStmt.executeQuery();
 			 return resultSetToObjectTransfer(rs); 
 		 } catch (Exception e) {
 			  throw e;
 		} finally {
 			 try { rs.close();
 			 pStmt.close();} catch (Exception e) {}
 			  
 		} 
 	} 

 	 public List<Tb_vendaT> getByData_venda(Tb_vendaT tb_vendaT) throws Exception { 
		 PreparedStatement pStmt=null;
		 ResultSet rs=null;
		 try {
			 String sql = "select * from tb_venda where  data_venda = ? "; 
			 pStmt = con.prepareStatement(sql);
		 java.sql.Date dt1= new java.sql.Date(tb_vendaT.getData_venda().getTime());
			 pStmt.setObject(1,dt1);
			 rs = pStmt.executeQuery();
 			 return resultSetToObjectTransfer(rs); 
 		 } catch (Exception e) {
 			  throw e;
 		} finally {
 			 try { rs.close();
 			 pStmt.close();} catch (Exception e) {}
 			  
 		} 
 	} 

 	 public List<Tb_vendaT> getByTipo_desconto(Tb_vendaT tb_vendaT) throws Exception { 
		 PreparedStatement pStmt=null;
		 ResultSet rs=null;
		 try {
			 String sql = "select * from tb_venda where  tipo_desconto = ? "; 
			 pStmt = con.prepareStatement(sql);
			 pStmt.setObject(1, tb_vendaT.getTipo_desconto());
			 rs = pStmt.executeQuery();
 			 return resultSetToObjectTransfer(rs); 
 		 } catch (Exception e) {
 			  throw e;
 		} finally {
 			 try { rs.close();
 			 pStmt.close();} catch (Exception e) {}
 			  
 		} 
 	} 

 	 public List<Tb_vendaT> getByDesconto(Tb_vendaT tb_vendaT) throws Exception { 
		 PreparedStatement pStmt=null;
		 ResultSet rs=null;
		 try {
			 String sql = "select * from tb_venda where  desconto = ? "; 
			 pStmt = con.prepareStatement(sql);
			 pStmt.setObject(1, tb_vendaT.getDesconto());
			 rs = pStmt.executeQuery();
 			 return resultSetToObjectTransfer(rs); 
 		 } catch (Exception e) {
 			  throw e;
 		} finally {
 			 try { rs.close();
 			 pStmt.close();} catch (Exception e) {}
 			  
 		} 
 	} 

 

 }